var cur_pos = [3,3];
var x;
var y;

var cmap = new Array(7);
for(var i = 0; i < 7; i ++){
  cmap[i] = new Array(4);
}

cmap[0][0] = "β";
cmap[0][1] = "A";
cmap[0][2] = "B";
cmap[0][3] = "C";
cmap[1][0] = "D";
cmap[1][1] = "E";
cmap[1][2] = "F";
cmap[1][3] = "G";
cmap[2][0] = "H";
cmap[2][1] = "I";
cmap[2][2] = "J";
cmap[2][3] = "K";
cmap[3][0] = "L";
cmap[3][1] = "M";
cmap[3][2] = "N";
cmap[3][3] = "α";
cmap[3][4] = "θ";
cmap[4][0] = "O";
cmap[4][1] = "P";
cmap[4][2] = "Q";
cmap[4][3] = "R";
cmap[5][0] = "S";
cmap[5][1] = "T";
cmap[5][2] = "U";
cmap[5][3] = "V";
cmap[6][0] = "W";
cmap[6][1] = "X";
cmap[6][2] = "Y";
cmap[6][3] = "Z";

function loadCT(cm){
    $(".color-cell").css("background-color", "#ffffff");
    $(".color-cell").unbind("click");
    var cells = $(".color-cell"); // getElementsByClass("color-cell");
    // alert(cells.length);
    for(var k = 0; k < 56; k++){
//	cells[k].get(0).cn = false;
	cells[k].cn = false;
    }
    for(var k = 1; k < 45; k++){
	$("#c"+ k).css("background-color", "#"+ ct[cm + k]);
	$("#c"+ k).data("cn",cm + k);
	$("#c"+ k).click(function(ev){
	    sampleColor(ev.target);
	});
    }
}

function loadTheta(){
    $(".color-cell-all").css("background-color", "#ffffff");
    var cells = $(".color-cell");
    for(var k = 0; k < 56; k++){
	cells[k].style.backgroundColor = "#"+ ct["δ" + (k+1)];
	cells[k].cn = "δ" + (k+1);
    }

   $(".map-cell").css("background-color", "#fff");

   $("#cmTheta").css("background-color", "#ddd");
   $(".color-cell-all").click(function(ev){
	   var cn = ev.target.cn;
	   $("#current-color").css("background-color", "#"+ct[cn]);
	   $("#rgb16").html(ct[cn]);
	   $("#rgb10").html(rgb10_ct[cn]);
	   $("#mancel").html(mancel_ct[cn]);
	   $("#excel-no").html("エクセルデータ No."+ color_num[cn]);
	   $("#cmyk").html(cmyk_ct[cn].replace(" Y", "<br />Y"));
       $("#current-color").data("cn", color_num[cn]);
       });
}

function change2sun(){
   getBack();
   $("#moon").attr("src","imgs/nav-moon.png");
   $("#back").attr("src","imgs/nav-back.png");
   $("#sun").attr("src","imgs/nav-sun-hl.png");
   $(".moon").hide();
   $(".cell-center").show();
   var sun_tds = $(".sun");
   $(".sun").animate({width: 82},300);
   return false;
}

function change2moon(){
   getBack();
   $("#moon").attr("src","imgs/nav-moon-hl.png");
   $("#back").attr("src","imgs/nav-back.png");
   $("#sun").attr("src","imgs/nav-sun.png");
   $(".sun").hide();
   $(".cell-center").show();
   $(".moon").animate({width: 82},300);
   return false;
}

function getBack(){
   $("#moon").attr("src","imgs/nav-moon.png");
   $("#back").attr("src","imgs/nav-back.png");
   $("#sun").attr("src","imgs/nav-sun.png");

   var moon_tds = $(".moon");
   for(var i=0;i<moon_tds.length;i++){
      moon_tds[i].style.display = "table-cell";
      moon_tds[i].style.width   = "40px";
      moon_tds[i].style.height   = "40px";
   }
   var sun_tds = $(".sun");
   for(var i=0;i<sun_tds.length;i++){
      sun_tds[i].style.display = "table-cell";
      sun_tds[i].style.width   = "40px";
      sun_tds[i].style.height   = "40px";
   }
   var center_tds = $(".cell-center");
   for(var i=0;i<center_tds.length;i++){
      center_tds[i].style.display = "table-cell";
      center_tds[i].style.width   = "80px";

   }
   return false;
}

var currentCT;
function changeCM(c){
  currentCT = c;
  // alert(c);
  $(".map-cell").css("background-color", "#ffffff");

  for(i = 0; i < 7; i++){
     for(j = 0; j < 4; j++){
        if(cmap[i][j] == c){
           $("#cm"+cmap[i][j]).css("background-color", "#dddddd");
        }
     }
  }
  loadCT(c);
}


function dragStartCt(event){
   x = event.clientX;
   y = event.clientY;
}

function dragEndCt(event){
   x = event.clientX - x;
   y = event.clientY - y;

   
   var td = ( y < 0 ? "u" : "d");
   var lr = ( x > 0 ? "r" : "l");
   var dir = (Math.abs(y/x) > 1.0 ? td : lr);
   if(dir ==  "u"){
      if(cur_pos[1] < 6){
         cur_pos[1] = cur_pos[1] +1;
      }
   }else if(dir == "d"){
      if(cur_pos[1] > 0){
         cur_pos[1] = cur_pos[1] - 1;
      }
   }else if(dir == "r"){
      if(cur_pos[0] > 0){
         cur_pos[0] = cur_pos[0] - 1;
      }
   }else if(dir == "l"){
      if(cur_pos[0] < 3){
         cur_pos[0] = cur_pos[0] + 1;
      }
   }
   changeCM(cmap[cur_pos[1]][cur_pos[0]]);
   
}



function sampleColor(cell){
   $("#current-color").css("background-color", cell.style.backgroundColor);
    var cn = (cell.cn ? cell.cn  : $("#"+cell.id).data("cn"));
   $("#rgb16").html(ct[cn]);
   $("#rgb10").html(rgb10_ct[cn]);
   $("#mancel").html(mancel_ct[cn]);
   $("#excel-no").html("エクセルデータ No."+ color_num[cn]);


   $("#cmyk").html(cmyk_ct[cn].replace(" Y", "<br />Y"));
   var k_value = cmyk_ct[cn].replace(/[^K]*K/, '');

   $("#current-color").data("cn", color_num[cn]);
}

function storeColor(ev){
}

function putPallet(obj){
}


var x,y;
jQuery.event.add(window, "load", function(){
  loadCT("α")
  titleObj = $("#title");
  header_height = titleObj.height();
  var img_html = '<img id="aun_color-img" src="imgs/aunColorIcon_big.jpg" width="'+
										 header_height +'" height="'+ header_height +'" />';

    titleObj.html(img_html + titleObj.html());
    $(".pallet-cell").each(function(){
	var timeout, longtouch;
	$(this).bind('touchstart', function() {
	    timeout = setTimeout(function() {
		longtouch = true;
	    }, 600);
	}).bind('touchend', function() {
	    if (longtouch) {
		$(this).css("background-color", "#fff");
		$(this).data("cn", false);
		
	    }else{
		$(this).css("background-color", $("#current-color").css("background-color"));
		$(this).data("cn", $("#current-color").data("cn"));
	    }
		
	    longtouch = false;
	    clearTimeout(timeout);
	});
	
    });

    $("#op-save").click(function(){
	var cells = $(".pallet-cell");
	var pallet_arr = new Array(5);
	var json_str = "\n{\n";
	for(var i = 0; i < cells.length; i++){
	    json_str += "   \"p"+(i+1) + "\": \"" + $("#"+ (cells[i].id)).data("cn") +"\", \n";
	}
	json_str = json_str.replace(/\, \n$/, "");
	json_str += "\n}\n\n";
	if(location.href.match(/^https?:\/\//)){
	    $.cookie("aun_color-data", escape(json_str));
	    alert("色情報が保存されました");
	}else{
	    $("#jsonStr").html("<pre>" +json_str +"</pre>");
	    $("#jsonStr").dialog();
	}
	
    });
    $("#op-load").click(function(){
	if(location.href.match(/^https?:\/\//)){
	    var json_str = unescape($.cookie("aun_color-data"));
	    var json_obj = jQuery.parseJSON(json_str);
	    var cells = $(".pallet-cell");
	    for(var i = 0; i < cells.length; i++){
		$("#p"+ (i+1)).css("background-color", num2bg[json_obj["p"+ (i+1)]]);
		$("#p"+ (i+1)).data("cn", json_obj["p"+ (i+1)]);
	    }
	}else{
	    $("#loadForm").dialog();
	}
	
    });
    $("#doLoad").click(function(){
	var json_str = document.getElementById("loadJson").value;
	var json_obj = jQuery.parseJSON(json_str);
	var cells = $(".pallet-cell");
	for(var i = 0; i < cells.length; i++){
	    $("#p"+ (i+1)).css("background-color", num2bg[json_obj["p"+ (i+1)]]);
	    $("#p"+ (i+1)).data("cn", json_obj["p"+ (i+1)]);
	}
    });
});


